import { Node, AudioSource, AudioClip, resources, director, log, Button, _decorator, Component, sys } from 'cc';
import { Consts } from '../Data/Consts';
import { bdlMgr } from './SS_BundleManager';
import { SS_Settings } from '../Data/SS_Settings';
const { ccclass, property } = _decorator;

/**
 * @en
 * this is a sington class for audio play, can be easily called from anywhere in you project.
 * @zh
 * 这是一个用于播放音频的单件类，可以很方便地在项目的任何地方调用。
 */
@ccclass('SS_AudioManager')
export class SS_AudioManager extends Component {
    static Instance: SS_AudioManager;

    @property(AudioSource)
    AudioSource_bgm: AudioSource | null = null;
    @property(AudioSource)
    AudioSource_effect: AudioSource | null = null;

    onLoad() {
        SS_AudioManager.Instance = this;

        this.OnHideAndShow();
    }

    // 特殊处理：KSGame 平台
    // 切后台暂停，切回来恢复音效
    OnHideAndShow() {
        const isKSGame = typeof window['KSGameGlobal'] != 'undefined';
        if (isKSGame) {
            ks.onShow((res) => {
                console.log("ks.onShow------------");
                this.resume();
            })
            ks.onHide(() => {
                console.log("ks.onHide------------");
                this.pause();
            })
        }
    }

    playOneShot(sound: AudioClip | string, volume: number = 1.0) {
        // if (sound instanceof AudioClip) {
        //     this.AudioSource_effect.playOneShot(sound, volume);
        // }
    }

    /**
     * @en
     * play long audio, such as the bg music
     * @zh
     * 播放长音频，比如 背景音乐
     * @param sound clip or url for the sound
     * @param volume 
     */
    play(sound: AudioClip | string, volume: number = 1.0) {
        // if (sound instanceof AudioClip) {
        //     this.AudioSource_bgm.stop();
        //     this.AudioSource_bgm.clip = sound;
        //     this.AudioSource_bgm.play();
        //     this.AudioSource_bgm.volume = volume;

        //     this.AudioSource_bgm.loop = true;
        // }
    }

    /**
     * stop the audio play
     */
    stop() {
        // this.AudioSource_bgm.stop();
    }

    /**
     * pause the audio play
     */
    pause() {
        // this.AudioSource_bgm.pause();
    }

    /**
     * resume the audio play
     */
    resume() {
        this.AudioSource_bgm.play();
    }

    static PlayButtonAudio() {
        // bdlMgr.getRes<AudioClip>(Consts.BundleName.AUDIO, `sound/按钮点击音效`, AudioClip).then(async (audio) => {
        //     SS_AudioManager.Instance.playOneShot(audio, SS_Settings.curMusicV);
        // });
    }
}